# -*- coding: utf-8 -*-
"""
@author:  yinbing.li(yinbing.li@zillnk.com)
@version: 1.0.0
@file:    leetcode567_字符串的排列.py
@time:    2023/3/9 16:11
"""
import collections


class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        # // s1长度大于s2长度，必定不成立
        if len(s1) > len(s2):
            return False

        start = 0
        end = len(s1)

        # // 用于存放s1字符的map
        s1_counter = collections.Counter(s1)
        # // 用于存放s2字符的map
        s2_counter = collections.Counter(s2[start:end])
        while end <= len(s2):
            if s1_counter == s2_counter:
                return True

            # 由于循环条件end <= s2.length()，=是为了取到s2末尾字符，当end=s2.length()时，s2.charAt(end)会发生越界，此处防止越界异常
            if end >= len(s2):
                break

            # // 滑动窗口右移动1个单位，当s2.charAt(end)存在时windowMap中时，让器计数值+1
            # // 当s2.charAt(start)存在时windowMap中时，因为窗口右移动，让其计数值-1，当计数值为0时，删除
            s2_counter[s2[end]] = s2_counter.get(s2[end], 0) + 1
            s2_counter[s2[start]] = s2_counter.get(s2[start]) - 1

            if s2_counter[s2[start]] == 0:
                s2_counter.pop(s2[start])

            start += 1
            end += 1
        return False


s = Solution()
print(s.checkInclusion("adc", "dcda"))